/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.utils;

import com.mortennobel.imagescaling.AdvancedResizeOp;
import com.mortennobel.imagescaling.ResampleOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author ECH
 */
public class ImageProcessor {
    
    //CX: 750 x 380
    public void createStandardSize(File photo) {    
        int width = 750;
        int height = 380;
        String path = photo.getParent();
        String prefix = "001-";
        String fileName = path.replaceAll("\\\\", "/") + "/" + prefix + photo.getName();
        
        String watermarkFileName = path.replaceAll("\\\\", "/") + "/" + "watermark.png";        
        BufferedImage src = null;
        try {                        
            src = ImageIO.read(photo);  
            if (src.getWidth() <= width) {
                width = src.getWidth();
            }
            if (src.getHeight() <= height) {
                height = src.getHeight();
            }                        
            //ResampleOp resampleOp = new ResampleOp (width, height);
            //resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
            
            //BufferedImage rescaled = resampleOp.filter(src, null);
            //ImageIO.write(rescaled, "JPG", new File(fileName));
                        
            Thumbnails.of(photo)
            .size(width, height)
            .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermarkFileName)), 0.5f)
            .outputQuality(0.8f)
            .toFile(new File(fileName));
            
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            
        }
    }
        
    public void createPreviewSize(File photo) {    
        int width = 330;
        int height = 170;
        String path = photo.getParent();
        String prefix = "002-";
        String fileName = path.replaceAll("\\\\", "/") + "/" + prefix + photo.getName();
                        
        try {
            BufferedImage src = ImageIO.read(photo);  
            
            //CX: portrait photo
            if (src.getHeight() > src.getWidth()) {
                height = 280;
                width = 330;
            }
            else {
                if (src.getWidth() <= width) {
                    width = src.getWidth();
                }
                if (src.getHeight() <= height) {
                    height = src.getHeight();
                }                
            }            
            
            ResampleOp resampleOp = new ResampleOp (width, height);
            //resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
            
            BufferedImage rescaled = resampleOp.filter(src, null);
            ImageIO.write(rescaled, "JPG", new File(fileName));
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }
    
    public static void main (String[] argv) {
        try {
            ImageProcessor processor = new ImageProcessor();
            
            /*
            File directory = new File("D:\\Chux\\Fotos");            
            File[] files = directory.listFiles();
            
            for (int i = 0; i < files.length; i++) {                
                
                File photo = files[i];
                //processor.createStandardSize(photo);           

                BufferedImage src = ImageIO.read(photo);            
                ResampleOp resampleOp = new ResampleOp (1200, 600);
                //resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
                BufferedImage rescaled = resampleOp.filter(src, null);
                ImageIO.write(rescaled, "JPG", new File("D:\\Chux\\Fotos\\HAM-" + photo.getName().replaceAll(".JPG", ".jpg")));                
            }
            */
            
            /*
            File f = new File("D:\\19_full.jpeg");
            processor.createStandardSize(f);           
            
            BufferedImage src = ImageIO.read(f);            
            ResampleOp resampleOp = new ResampleOp (800, 600);
            //resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
            BufferedImage rescaled = resampleOp.filter(src, null);
            ImageIO.write(rescaled, "JPG", new File("D:\\19_full_HAM.jpeg"));
            */
            
            Thumbnails.of(new File("D:\\001-res7-search-bg-6.JPG"))
            .size(750, 380)
            .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("D:\\watermark.png")), 0.5f)
            .outputQuality(0.8f)
            .toFile(new File("D:\\image-with-watermark.jpg"));
            
        } 
        catch (IOException ex) {
            //Logger.getLogger(XSnappy.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
}
